package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.mapper.CheckItemMapper;
import com.health.pojo.CheckItem;
import com.health.request.CheckItemDto;
import com.health.response.PageResult;
import com.health.response.Result;
import com.health.service.CheckItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : Areli
 * @date : 2020/4/16 21:47
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    CheckItemMapper checkItemMapper;

    /**
     * 查询所有
     */
    @Override
    public Result<List<CheckItem>> findAll() {
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        if (checkItemList == null | checkItemList.size() ==0 ){
            return new Result<>("查无信息");
        }
        return new Result<>(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

    /**
     * 根据id查询
     * @param id
     */
    @Override
    public Result<CheckItem> findOne(Integer id) {
        CheckItem item = checkItemMapper.findOne(id);
        if (item == null){
            return new Result<>("查无此检查项");
        }
        return new Result<>(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,item);
    }

    /**
     * 分页条件查询
     * @param checkItemDto
     * @param page
     * @param pageSize
     */
    @Override
    public Result search(CheckItemDto checkItemDto, Integer page, Integer pageSize) {
        if (page == null || page < 1){
            page =  1;
        }
        if (pageSize == null || pageSize < 1){
            pageSize = 10;
        }
        CheckItem checkItem = new CheckItem();
        BeanUtils.copyProperties(checkItemDto,checkItem);
        PageHelper.startPage(page,pageSize);
        Page<CheckItem> pageList = (Page<CheckItem>) checkItemMapper.search(checkItem);
        if (pageList == null || pageList.getTotal() ==0){
            return new Result("查询失败");
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageList.getTotal());
        pageResult.setRows(pageList.getResult());
        return new Result(pageResult);
    }

    /**
     * 新增
     * @param checkItem
     */
    @Override
    public Result<CheckItem> add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
        return new Result<>(true,MessageConstant.ADD_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * 根据id修改
     * @param id
     * @param checkItem
     */
    @Override
    public Result<CheckItem> edit(Integer id, CheckItem checkItem) {
        CheckItem item = checkItemMapper.findOne(id);
        if (item == null){
            return new Result<>("检查项不存在");
        }
        checkItem.setId(id);
        checkItemMapper.edit(checkItem);
        return new Result<>(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        checkItemMapper.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,null);
    }
}
